var Net = require("Net")
var Global = require("Global")
cc.Class({
    extends: cc.Component,

    properties: {
        lblName:cc.Label,
        lblMoney:cc.Label,
        lblGems:cc.Label,
        lblID:cc.Label,
        lblNotice:cc.Label,
        joinGameWin:cc.Node,
        createRoomWin:cc.Node,
        settingsWin:cc.Node,
        helpWin:cc.Node,
        xiaoxiWin:cc.Node,
        btnJoinGame:cc.Node,
        btnReturnGame:cc.Node,
        sprHeadImg:cc.Sprite,
        share:cc.Node,
    },
    
    initNetHandlers:function(){
        var self = this;
    },
    
    onShare:function(){
        this.share.active = true;
        var sharePY = cc.find("Canvas/share/sharePY");
        var sharePYQ = cc.find("Canvas/share/sharePYQ");
        var btncolse = cc.find("Canvas/share/btncolse");
        cc.vv.utils.addClickEvent(sharePYQ,this.node,"Hall","onBtnClicked");
        cc.vv.utils.addClickEvent(sharePY,this.node,"Hall","onBtnClicked");
        cc.vv.utils.addClickEvent(btncolse,this.node,"Hall","onBtnClicked");
        // cc.vv.anysdkMgr.share("有朋温州麻将","一款温州地区流行特色麻将玩法。");   
    },

    // use this for initialization
    onLoad: function () {
        console.log("onLoad of Hall.js");
        if(!cc.vv){
            cc.director.loadScene("loading");
            return;
        }
        this.initLabels();
        
        if(cc.vv.gameNetMgr.roomId == null){
            this.btnJoinGame.active = true;
            this.btnReturnGame.active = false;
        }
        else{
            this.btnJoinGame.active = false;
            this.btnReturnGame.active = true;
        }
        
        //var params = cc.vv.args;
        var roomId = cc.vv.userMgr.oldRoomId 
        if( roomId != null){
            cc.vv.userMgr.oldRoomId = null;
            cc.vv.userMgr.enterRoom(roomId);
        }
        
        var imgLoader = this.sprHeadImg.node.getComponent("ImageLoader");
        // imgLoader.setUserID(cc.vv.userMgr.userId);
        // console.log("--------------------------------------------------------------");
        // console.log("login_id="+cc.vv.userMgr.login_id+" img_url="+cc.vv.userMgr.img_url);
        imgLoader.setUserIDOfInfo(cc.vv.userMgr.login_id,cc.vv.userMgr.img_url);
        // if(cc.vv.userMgr.login_id){
        //     console.log("--------------------------------------------------------------");
        //     imgLoader.setUserIDOfInfo(cc.vv.userMgr.login_id,cc.vv.userMgr.img_url);
        // }
        cc.vv.utils.addClickEvent(this.sprHeadImg.node,this.node,"Hall","onBtnClicked");
        
        
        this.addComponent("UserInfoShow");
        
        this.initButtonHandler("Canvas/btn_shezhi");
        this.initButtonHandler("Canvas/btn_help");
        this.initButtonHandler("Canvas/right_bottom/btn_xiaoxi");
        var btnJoinGame = cc.find("Canvas/btnJoinGame");
        cc.vv.utils.addClickEvent(btnJoinGame,this.node,"Hall","onJoinGameClicked",1);
        var btnReturnGame = cc.find("Canvas/btnReturnGame");
        cc.vv.utils.addClickEvent(btnReturnGame,this.node,"Hall","onReturnGameClicked",1);
        var btn_create_room = cc.find("Canvas/btn_create_room");
        cc.vv.utils.addClickEvent(btn_create_room,this.node,"Hall","onCreateRoomClicked",1);
        var btn_share = cc.find("Canvas/btn_share");
        cc.vv.utils.addClickEvent(btn_share,this.node,"Hall","onShare",1);
        this.helpWin.addComponent("OnBack");
        this.xiaoxiWin.addComponent("OnBack");
        this.share = cc.find("Canvas/share");
        if(!cc.vv.userMgr.notice){
            cc.vv.userMgr.notice = {
                version:null,
                msg:"数据请求中...",
            }
        }
        
        if(!cc.vv.userMgr.gemstip){
            cc.vv.userMgr.gemstip = {
                version:null,
                msg:"数据请求中...",
            }
        }
        
        this.lblNotice.string = cc.vv.userMgr.notice.msg;
        
        this.refreshInfo();
        this.refreshNotice();
        this.refreshGemsTip();

        var self=this;
        this.node.on("refreshGemTip_push",function(){
            console.log("refreshGemTip_push");
            // self.refreshGemsTip();
            self.lblGems.string = cc.vv.userMgr.gems;
        });

        this.node.on("close_share_win_push",function(){
            console.log("close_share_win_push");
            self.share.active=false;
        });
        
        // cc.vv.audioMgr.playBGM("bgMain.mp3");
    },
    
    start:function(){

        cc.vv.hallHandle=this.node;
        // cc.log("start:cc.vv.isReconnect="+cc.vv.isReconnect,cc.vv.userMgr.roomData_reconnect);
        if(cc.vv.isReconnect /*&& cc.vv.userMgr.roomData_reconnect != null*/){
            console.log("connectGameServer of update function/Hall.js");
            cc.vv.userMgr.reEnterRoom(function(event){
                if(!event){
                    console.log("enter room failed");
                    cc.vv.alert.show("提示","进入房间失败,请重新登陆！",function(){
                        cc.director.loadScene("login");
                    });
                    return;
                }
                console.log("---------------------------------------"+cc.vv.gameNetMgr.gamestate);
                if(!cc.vv.setPeople.isERMJ()&&cc.vv.gameNetMgr.gamestate=="playing"){
                    console.log("----------------------------------cc.vv.isFourMen="+cc.vv.isFourMen);
                    cc.vv.isFourMen=true;
                    console.log("----------------------------------cc.vv.isFourMen="+cc.vv.isFourMen);
                }
                // cc.vv.ifFourMen=true;
                cc.vv.gameNetMgr.connectGameServer(event);
            });
            cc.vv.wc.show("正在获取信息");
            cc.vv.timeout.timeoutOne(function(){
                console.log("once action ---------------------------------------------");
                cc.vv.wc.hide();
                cc.vv.alert.show("提示","获取信息失败！\n请检查网络后重新登陆！",function(){
                    cc.director.loadScene("login");
                });

            },this,7);
            // cc.vv.gameNetMgr.connectGameServer(cc.vv.userMgr.roomData_reconnect);
            // cc.vv.userMgr.roomData = null;
            cc.vv.isReconnect=false;
        }  
    },
    
    refreshInfo:function(){
        console.log("refreshInfo of Hall.js");
        var self = this;
        var onGet = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg);
            }
            else{
                if(ret.gems != null){
                    this.lblGems.string = ret.gems;
                    console.log("ret.gems="+ret.gems);    
                }
            }
        };
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
        };
        cc.vv.http.sendRequest("/get_user_status",data,onGet.bind(this));
    },
    
    refreshGemsTip:function(){
        console.log("refreshGemsTip of Hall.js");
        var self = this;
        var onGet = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg);
            }
            else{
                console.log("ret="+ret);
                cc.vv.userMgr.gemstip.version = ret.version;
                cc.vv.userMgr.gemstip.msg = ret.msg.replace("<newline>","\n");
            }
        };
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
            type:"fkgm",
            version:cc.vv.userMgr.gemstip.version
        };
        cc.vv.http.sendRequest("/get_message",data,onGet.bind(this));
    },
    
    refreshNotice:function(){
        console.log("refreshNotice of Hall.js");
        var self = this;
        var onGet = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg);
            }
            else{
                console.log("ret="+ret);
                cc.vv.userMgr.notice.version = ret.version;
                cc.vv.userMgr.notice.msg = ret.msg;
                this.lblNotice.string = ret.msg;
            }
        };
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
            type:"notice",
            version:cc.vv.userMgr.notice.version
        };
        cc.vv.http.sendRequest("/get_message",data,onGet.bind(this));
    },
    
    initButtonHandler:function(btnPath){
        var btn = cc.find(btnPath);
        cc.vv.utils.addClickEvent(btn,this.node,"Hall","onBtnClicked",1);        
    },
    
    
    
    initLabels:function(){
        this.lblName.string = cc.vv.gameNetMgr.setName(cc.vv.userMgr.userName,8,true);
        this.lblMoney.string = cc.vv.userMgr.coins;
        this.lblGems.string = cc.vv.userMgr.gems;
        this.lblID.string = "ID:" + cc.vv.userMgr.userId;
    },
    
    onBtnClicked:function(event){
        if(event.target.name == "btn_shezhi"){
            // console.log("share");
            // var fs = function(ret)
            //     {
            //         console.log(ret);
            //         cc.vv.userMgr.gems = ret.account;
            //         console.log(ret.account);
            //     }
            //     cc.vv.http.sendRequest("/getGems",{userId:userId},fs);

            this.settingsWin.active = true;
        }   
        else if(event.target.name == "btn_help"){
            this.helpWin.active = true;
        }
        else if(event.target.name == "btn_xiaoxi"){
            this.xiaoxiWin.active = true;
        }
        else if(event.target.name == "head"){
            cc.vv.userinfoShow.show(cc.vv.userMgr.userName,cc.vv.userMgr.userId,this.sprHeadImg,cc.vv.userMgr.sex,cc.vv.userMgr.ip);
        }else if(event.target.name == "sharePYQ")
        {
            console.log("pnegyouquan");
            var type = false;
            cc.vv.anysdkMgr.share("有朋温州麻将","一款温州地区流行特色麻将玩法。",type); 
        }else if(event.target.name == "sharePY")
        {
            var type = true;
            console.log("pnegyouquan111");
            cc.vv.anysdkMgr.share("有朋温州麻将","一款温州地区流行特色麻将玩法。",type); 
        }
        else if(event.target.name == "btncolse")
        {
            this.share.active = false;
        }
    },
    
    onJoinGameClicked:function(){
        this.joinGameWin.active = true;
        // cc.vv.audioMgr.playClick_buttomSFX();
    },
    
    onReturnGameClicked:function(){
        if(cc.vv.userMgr.roomData_reconnect){
            cc.log("enter mjgame");
            cc.vv.gameNetMgr.connectGameServer(cc.vv.userMgr.roomData_reconnect);
            return;
        }
        cc.director.loadScene("mjgame"); 
    },
    
    onBtnAddGemsClicked:function(){
        cc.vv.alert.show("提示",cc.vv.userMgr.gemstip.msg); 
    },
    
    onCreateRoomClicked:function(){
        console.log("onCreateRoomClicked");
        if(cc.vv.gameNetMgr.roomId != null){
            cc.vv.alert.show("提示","房间已经创建!\n必须解散当前房间才能创建新的房间");
            return;
        }
        this.createRoomWin.active = true;  
        // cc.director.loadScene("mjhall");

        // var self=this;
        // console.log("createRoom");
        // self.createRoom();
    },
    
    //  createRoom:function(){
    //     var self = this;
    //     var onCreate = function(ret){
    //         if(ret.errcode !== 0){
    //             cc.vv.wc.hide();
    //             //console.log(ret.errmsg);
    //             if(ret.errcode == 2222){
    //                 cc.vv.alert.show("提示","房卡不足，创建房间失败!");  
    //             }
    //             else{
    //                 cc.vv.alert.show("提示","创建房间失败,错误码:" + ret.errcode);
    //             }
    //         }
    //         else{
    //             cc.vv.gameNetMgr.connectGameServer(ret);
    //         }
    //     };
        
    //     var difen = 0;
    //     // for(var i = 0; i < self._difenxuanze.length; ++i){
    //     //     if(self._difenxuanze[i].checked){
    //     //         difen = i;
    //     //         break;
    //     //     }
    //     // }
        
    //     var zimo = 0;
    //     // for(var i = 0; i < self._zimo.length; ++i){
    //     //     if(self._zimo[i].checked){
    //     //         zimo = i;
    //     //         break;
    //     //     }     
    //     // }

    //     var huansanzhang = self._wanfaxuanze[0].checked;        
    //     var jiangdui = self._wanfaxuanze[1].checked;
    //     var menqing = self._wanfaxuanze[2].checked;
    //     var tiandihu = self._wanfaxuanze[3].checked;
        
    //     var type = 0;
    //     for(var i = 0; i < self._leixingxuanze.length; ++i){
    //         if(self._leixingxuanze[i].checked){
    //             type = i;
    //             break;
    //         }     
    //     }
        
    //     if(type == 0){
    //         type = "xzdd";
    //     }
    //     else{
    //         type = "xlch";
    //     }
        
    //     var zuidafanshu = 0;
    //     for(var i = 0; i < self._zuidafanshu.length; ++i){
    //         if(self._zuidafanshu[i].checked){
    //             zuidafanshu = i;
    //             break;
    //         }     
    //     }
        
        
    //     var jushuxuanze = 0;
    //     for(var i = 0; i < self._jushuxuanze.length; ++i){
    //         if(self._jushuxuanze[i].checked){
    //             jushuxuanze = i;
    //             break;
    //         }     
    //     }
        
    //     var dianganghua = 0;
    //     for(var i = 0; i < self._dianganghua.length; ++i){
    //         if(self._dianganghua[i].checked){
    //             dianganghua = i;
    //             break;
    //         }     
    //     }
        
    //     var conf = {
    //         type:type,
    //         difen:difen,
    //         zimo:zimo,
    //         jiangdui:jiangdui,
    //         huansanzhang:huansanzhang,
    //         zuidafanshu:zuidafanshu,
    //         jushuxuanze:jushuxuanze,
    //         dianganghua:dianganghua,
    //         menqing:menqing,
    //         tiandihu:tiandihu,   
    //     }; 
        
    //     var data = {
    //         account:cc.vv.userMgr.account,
    //         sign:cc.vv.userMgr.sign,
    //         conf:JSON.stringify(conf)
    //     };
    //     console.log(data);
    //     cc.vv.wc.show("正在创建房间");
    //     cc.vv.http.sendRequest("/create_private_room",data,onCreate);   
    // },

    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        var x = this.lblNotice.node.x;
        x -= dt*100;
        if(x + this.lblNotice.node.width < -1000){
            x = 500;
        }
        this.lblNotice.node.x = x;
        
        if(cc.vv && cc.vv.userMgr.roomData != null){
            console.log("connectGameServer of update function/Hall.js");
            cc.vv.gameNetMgr.connectGameServer(cc.vv.userMgr.roomData);
            cc.vv.userMgr.roomData = null;
        }
    },
});
